<?
require 'global.php';

// product id. 产品id
$id=0; if(isset($_GET["id"]))if(is_numeric($_GET["id"]))$id=$_GET["id"];
if($id==0) { header('Location:./'); exit; }

$product_obj=$item=$product->get_row($id);
if(is_null($item))  { header('Location:404.html'); exit; }
$product_price=price($item);

$product_review_summary_obj=$product_review->get_summary($id);
$product_pic_obj=$product_pic->get_rows(10,"product_id",$id,"seq_no","desc");

$product_variation_obj=$product_variation->get_rows(0,"product_id",$id,"id","asc");
$colorVariation=array();
$sizeVariation=array();
$other1Variation=array();
$other2Variation=array();
foreach($product_variation_obj as $row){
	if($row['color']>0) {
		$colorVariation[$row['color']]=$option_value[$row['color']];
	}
	if($row['size']>0) {
		$sizeVariation[$row['size']]=$option_value[$row['size']];
	}
	if($row['other1']>0) {
		$other1Variation[$row['other1']]=$option_value[$row['other1']];
	}
	if($row['other2']>0) {
		$other2Variation[$row['other2']]=$option_value[$row['other2']];
	}	
}
asort($colorVariation);
asort($sizeVariation);
asort($other1Variation);
asort($other2Variation);
//print_r($colorVariation);
//print_r($sizeVariation);
//print_r($other1Variation);
//print_r($other2Variation);

// 相关产品 related products
$tem = $cross_sell->get_rows(32,'product_id',$id);
$in_arr=array();
foreach($tem as $row) $in_arr[]=$row['cross_product_id'];
$where=array();
$where[]=array('name'=>'id','val'=>$in_arr,'opn'=>'in');
$where[]=array('name'=>'stock','val'=>0,'opn'=>'>');
$tem = $product->get_rows_advanced(16,$where);
$arr_related=array();
$k=0;
//shuffle($tem);
foreach($tem as $row) { $arr_related[]=$row; $k++; if($k==MAX_RELATED_PRODUCT_SHOWN) break; }
if(ENABLE_CATEGORY_CROSS_SELL){
	//if not enough, get from same category
	if($k<MAX_RELATED_PRODUCT_SHOWN){
		$where=array();
		$where[]=array('name'=>'id','val'=>$product_obj['id'],'opn'=>'<>');
		$where[]=array('name'=>'is_enabled','val'=>1);
		$where[]=array('name'=>'category_id','val'=>$product_obj['category_id']);
		$tem = $product->get_rows_advanced(32,$where);
		shuffle($tem);
		foreach($tem as $row) { 
			if(! in_array($row['id'],$in_arr)){ $arr_related[]=$row; $k++;}
			if($k==MAX_HOT_PRODUCT_SHOWN) break;
		}
	}
}

// 热销产品 hot_products
$tem = $product->get_rows(16,'is_enabled',1,'sold_quantity','desc');
shuffle($tem);
$arr_hot=array();
$k=0;
foreach($tem as $row) { $arr_hot[]=$row; $k++; if($k==MAX_HOT_PRODUCT_SHOWN) break; }

//标签
$where_t = array();
$where_t[] = array('name'=>'p_id', 'val'=>$id, 'opn'=>'=', 'and'=>'and');
$where_t[] = array('name'=>'tags_table', 'val'=>1, 'opn'=>'=', 'and'=>'and');
$tags_list_info = $tag_list->get_rows_advanced(1024, $where_t);
$tag_info = array();
foreach($tags_list_info as $key_t => $val_t){
    $tag_info[] = $tags->get_row($val_t['tag_id']);
}

// parents for breadcrumb 父级分类和面包屑导航
$cateid=$product_obj['category_id'];
$cate_parents = get_level_parents($tree_product_category,$cateid);
$cate_parents = array_reverse($cate_parents);

//increment hit count 增加产品点击
$product->click($id);
add_to_history($id);

// 最大购买数量和库存取最小值
$max_buy=$item['stock'];
if($max_buy > MAX_PURCHASE_QUANTITY) $max_buy = MAX_PURCHASE_QUANTITY;
// 商品单位
$arr_unit = $code_array['product_unit'];
$product_unit = $arr_unit[$item['unit']]['name_'.$lang];

// SEO 设置 seo tags
$title=$product_obj['name_'.$lang];
$kwd=$title;
$dpn=$kwd;

// view 加载视图
require VIEW.'product.php';

// end of file